package com.zhixun.mobile.modules.jiujiudevice.device.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.boxin.ims.modules.jiujiudevice.entity.JiuInkRecord;
import com.google.common.collect.Lists;
import com.zhixun.mobile.modules.jiujiudevice.device.model.DataModel;
import com.zhixun.mobile.modules.jiujiudevice.device.model.FenduanModel;
import com.zhixun.mobile.modules.jiujiudevice.device.model.InkModel;

public class test2 {

	public static void main(String[] args) {
		/*
		 * String a = "10g"; String b = "20g";
		 * System.out.println(Double.parseDouble(b.substring(0,b.length()-1))-
		 * Double.parseDouble(a.substring(0,a.length()-1))); List<Integer> l =
		 * new ArrayList<Integer>(); l.add(6); l.add(5); l.add(2); l.add(8);
		 * l.add(2); int d = 0; for(int i = 0; i < l.size() ; i++) { if((i+1) <
		 * (l.size())){ if(l.get(i) > l.get(i+1)) { d += (l.get(i) -
		 * l.get(i+1)); } } } System.out.println(d); List<String> JiuOrderLog j
		 * = new JiuOrderLog(); j.setInkMessage(); String str = new
		 * String('[{"id":"7","price":0.01,"sn":"869696043668891","useMoney":0.
		 * 001,"useValue":100,"value":0},{"id":"6","price":0.01,"sn":
		 * "869696043668891","useMoney":0,"useValue":0,"value":50}]');
		 * 
		 * Double d= 0.155d; double b = new BigDecimal(d).setScale(2,
		 * BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(b);
		 */
		// {
		// "sn":"861510030498717",
		// "sta1":"1","sta2":"1",
		// "v1":"1234","v2":"1234"
		// "v3":"1234"
		// "v4":"1234"
		// "v5":"1234"
		//
		//
		// "v6":"1234"
		// "v7":"1234"
		// "v8":"1234"
		// }
		/*
		 * Map<String,Object> l = new HashMap<String,Object>(); l.put("sn",
		 * "861510030498717"); l.put("sta1", "1"); l.put("sta2", "1");
		 * l.put("v1", "100"); l.put("v2", "200"); l.put("v3", "300");
		 * l.put("v4", "400"); l.put("v5", "500"); l.put("v6", "600");
		 * l.put("v7", "700"); l.put("v8", "800");
		 * 
		 * a.putAll(l); a.putAll(l); System.out.println(a);
		 */
		


       //模拟从数据库取值
		
		List<JiuInkRecord> someink = new ArrayList<JiuInkRecord>();

		
		//设置总的
		JiuInkRecord j = new JiuInkRecord();
		j.setInkStatus("0");
		//模拟DataModel数据
		// 设置各路线的数据
		List<DataModel> dList1 = Lists.newArrayList();
		// 设置8路的数据
		for (int i = 1; i <= 8; i++) {
			DataModel data = new DataModel();
			data.setId(i + "");
			data.setValue(10d);
			dList1.add(data);
		}
        j.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j);
		
		
		JiuInkRecord j1 = new JiuInkRecord();
		j1.setInkStatus("0");
		//模拟DataModel数据
				// 设置各路线的数据
				 dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(9d);
					dList1.add(data);
				}
				
				
		        j1.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j1);
		
		
	    j1= new JiuInkRecord();
			j1.setInkStatus("0");
			//模拟DataModel数据
					// 设置各路线的数据
				dList1 = Lists.newArrayList();
					// 设置8路的数据
					for (int i = 1; i <= 8; i++) {
						DataModel data = new DataModel();
						data.setId(i + "");
						data.setValue(10d);
						dList1.add(data);
					}
			        j1.setInkMessage(JSON.toJSONString(dList1));
			someink.add(j1);    
			
			
		    j1= new JiuInkRecord();
				j1.setInkStatus("0");
				//模拟DataModel数据
						// 设置各路线的数据
					dList1 = Lists.newArrayList();
						// 设置8路的数据
						for (int i = 1; i <= 8; i++) {
							DataModel data = new DataModel();
							data.setId(i + "");
							data.setValue(9d);
							dList1.add(data);
						}
				        j1.setInkMessage(JSON.toJSONString(dList1));
				someink.add(j1);  
			
			
			j1 = new JiuInkRecord();
			j1.setInkStatus("0");
			//模拟DataModel数据
					// 设置各路线的数据
				dList1 = Lists.newArrayList();
					// 设置8路的数据
					for (int i = 1; i <= 8; i++) {
						DataModel data = new DataModel();
						data.setId(i + "");
						data.setValue(9d);
						dList1.add(data);
					}
			        j1.setInkMessage(JSON.toJSONString(dList1));
			someink.add(j1);
		JiuInkRecord j2 = new JiuInkRecord();
		j2.setInkStatus("1");
		//模拟DataModel数据
				// 设置各路线的数据
				dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(0d);
					dList1.add(data);
				}
		        j2.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j2);
		
		JiuInkRecord j3 = new JiuInkRecord();
		j3.setInkStatus("0");
		//模拟DataModel数据
				// 设置各路线的数据
		dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(10d);
					dList1.add(data);
				}
		        j3.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j3);
		
		JiuInkRecord j4 = new JiuInkRecord();
		j4.setInkStatus("0");
		//模拟DataModel数据
				// 设置各路线的数据
				 dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(9d);
					dList1.add(data);
				}
		        j4.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j4);
		
		JiuInkRecord j5 = new JiuInkRecord();
		j5.setInkStatus("1");
		//模拟DataModel数据
				// 设置各路线的数据
				dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(10d);
					dList1.add(data);
				}
		        j5.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j5);
		
		JiuInkRecord j6 = new JiuInkRecord();
		j6.setInkStatus("0");
		//模拟DataModel数据
				// 设置各路线的数据
			dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(8d);
					dList1.add(data);
				}
		        j6.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j6);
		
		JiuInkRecord j7 = new JiuInkRecord();
		j7.setInkStatus("0");
		//模拟DataModel数据
				// 设置各路线的数据
				 dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(7d);
					dList1.add(data);
				}
		        j7.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j7);
		
		JiuInkRecord j8 = new JiuInkRecord();
		j8.setInkStatus("0");
		//模拟DataModel数据
				// 设置各路线的数据
			dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(5d);
					dList1.add(data);
				}
		        j8.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j8);
//		JiuInkRecord j9 = new JiuInkRecord();
//		j9.setInkStatus("1");
//		someink.add(j9);
//		JiuInkRecord j10 = new JiuInkRecord();
//		j10.setInkStatus("1");
//		someink.add(j10);
		/*JiuInkRecord j11 = new JiuInkRecord();
		j11.setInkStatus("0");
		someink.add(j11);*/
	     j6 = new JiuInkRecord();
		j6.setInkStatus("1");
		//模拟DataModel数据
				// 设置各路线的数据
			dList1 = Lists.newArrayList();
				// 设置8路的数据
				for (int i = 1; i <= 8; i++) {
					DataModel data = new DataModel();
					data.setId(i + "");
					data.setValue(10d);
					dList1.add(data);
				}
		        j6.setInkMessage(JSON.toJSONString(dList1));
		someink.add(j6);
		 j6 = new JiuInkRecord();
			j6.setInkStatus("0");
			//模拟DataModel数据
					// 设置各路线的数据
				dList1 = Lists.newArrayList();
					// 设置8路的数据
					for (int i = 1; i <= 8; i++) {
						DataModel data = new DataModel();
						data.setId(i + "");
						data.setValue(9d);
						dList1.add(data);
					}
			        j6.setInkMessage(JSON.toJSONString(dList1));
			someink.add(j6);
			 j6 = new JiuInkRecord();
				j6.setInkStatus("0");
				//模拟DataModel数据
						// 设置各路线的数据
					dList1 = Lists.newArrayList();
						// 设置8路的数据
						for (int i = 1; i <= 8; i++) {
							DataModel data = new DataModel();
							data.setId(i + "");
							data.setValue(0d);
							dList1.add(data);
						}
				        j6.setInkMessage(JSON.toJSONString(dList1));
				someink.add(j6);
				
				 j6 = new JiuInkRecord();
					j6.setInkStatus("0");
					//模拟DataModel数据
							// 设置各路线的数据
						dList1 = Lists.newArrayList();
							// 设置8路的数据
							for (int i = 1; i <= 8; i++) {
								DataModel data = new DataModel();
								data.setId(i + "");
								data.setValue(0d);
								dList1.add(data);
							}
					        j6.setInkMessage(JSON.toJSONString(dList1));
					someink.add(j6);
		//遍历总的
		for (int i = 0; i < someink.size(); i++) {
			JiuInkRecord m  = someink.get(i);
			System.out.println("存的；"+m.getInkStatus());
		}
		//取值结束
		
		
		
		/**计算**/
		//分段总的list
		List<FenduanModel<JiuInkRecord>> fen = new ArrayList<FenduanModel<JiuInkRecord>>();
		
		//一直维持最后一条数据，实时更新
		FenduanModel<JiuInkRecord> fm = new FenduanModel<JiuInkRecord>();
		List<JiuInkRecord> fenList = new ArrayList<JiuInkRecord>();
		//单个设置
		for (int i = 0; i < someink.size(); i++) {
			if((i+1)<(someink.size())){
				JiuInkRecord jir = someink.get(i);
//				System.out.println(jir.getInkStatus());
				JiuInkRecord jir2 = someink.get(i+1);
//				System.out.println(jir2.getInkStatus());
				if(jir.getInkStatus().equals(jir2.getInkStatus())){
					fenList.add(jir);
				}else{
					fenList.add(jir);
					fm.setDatalist(fenList);
					//fm.setSubmoney(0d);
					fen.add(fm);
					fenList = new ArrayList<JiuInkRecord>(); 
//					if(jir.getInkStatus().equals("1") && jir2.getInkStatus().equals("0")){
//						fenList.add(fm.getDatalist().get(fm.getDatalist().size()-1));
//					}
					fm = new FenduanModel<JiuInkRecord>();
					continue;
				}
			}else{
				fenList.add(someink.get(someink.size()-1));
				fm.setDatalist(fenList);
				//fm.setSubmoney(0d);
				fen.add(fm);
				fenList = new ArrayList<JiuInkRecord>(); 
				fm = new FenduanModel<JiuInkRecord>();
			}
		}
		
		System.out.println(fen.size()+"存的list个数");
		
	
		//八路总的值
		int t = 0;
		for (int i = 0; i < fen.size(); i++) {
			FenduanModel<JiuInkRecord> f = fen.get(i);
			double [] d = new double[10];
			for (int k = 0; k < f.getDatalist().size(); k++) {
				if(f.getDatalist().size() >= 2){	
				// 这个LIST是上一次的 8 路数据
					List<DataModel> dataList1 = JSON.parseArray(f.getDatalist().get(0).getInkMessage(),DataModel.class);
					// 这个是当前的8路数据 JSON.parseArray(f.getDatalist().get(k+1).getInkMessage(),DataModel.class);
					List<DataModel> dataList2 =JSON.parseArray(f.getDatalist().get(f.getDatalist().size()-1).getInkMessage(),DataModel.class);
          
					// 用上一次的8路数据 分别减去当前的8路数据
					double []  totalValue= chuliData(dataList1, dataList2,d);
					t = dataList1.size();
					//设置八路的总价,取8路
					double total = 0d;
					for (int l = 0; l < t; l++) {
						total += (totalValue[l]/1000)*10;
					}
					
					f.setSubmoney(new BigDecimal(total).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
			 }
			}
		}
		
		//加入money后的fen
		double d2 = 0d;//总价
		for (int i = 0; i < fen.size(); i++) {
			FenduanModel<JiuInkRecord> f = fen.get(i);
			System.out.println("第"+(i+1)+"组========================>");
			System.out.println("合计》》"+f.getSubmoney());
			if(f.getSubmoney()!=null){
				d2 = f.getSubmoney();
			}
			for (int k = 0; k < f.getDatalist().size(); k++) {
				JiuInkRecord jk = f.getDatalist().get(k);
				 System.out.println("墨盒数据："+jk.getInkMessage());
				 System.out.println("墨盒状态："+jk.getInkStatus());
				}
		}
		System.out.println("d2="+new BigDecimal(d2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
	}

	private static double[] chuliData(List<DataModel> dataList1, List<DataModel> dataList2,double[]d) {
		// TODO Auto-generated method stub
		for (int i = 0; i < dataList1.size(); i++) {
			// 这是计算每一路的 相邻的数据查 如果后者比前者大 不计算
//			if (dataList1.get(i).getValue() > dataList2.get(i).getValue()) {

				// 如果d[i] == 0说明就是某一路的初始值
					d[i] = dataList1.get(i).getValue() - dataList2.get(i).getValue();
			
		//}
		}
		return d;
	}
}
